﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Cube2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        #region Field
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
       
        //class model
        
       
        CCamera camera2;
        MouseState lastMouseState;
        SpriteFont fontArial;

        Ship ship;        
        #endregion

        
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

      
        protected override void Initialize()
        {
            ship = new Ship();
            base.Initialize();
        }

     
        protected override void LoadContent()
        {           
            spriteBatch = new SpriteBatch(GraphicsDevice);

            ship.LoadContent(Content, GraphicsDevice);
            camera2 = new FreeCamera(new Vector3(1000, 0, -2000), MathHelper.ToRadians(153), MathHelper.ToRadians(5), GraphicsDevice);                     
            lastMouseState = Mouse.GetState();
           
            fontArial = Content.Load<SpriteFont>("Font\\Arial");
        }

      
        protected override void UnloadContent()
        {           
            Content.Unload();
        }
      
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
                      
            #region CLLX
            //if (Keyboard.GetState().IsKeyDown(Keys.Q))
            //MoveCLLX(gameTime);
            //UpdateFreeCamera();
            #endregion
            ship.Update(gameTime);
            
            base.Update(gameTime);
        }
       
        #region CLLX
        //public void MoveCLLX(GameTime gameTime)
        //{ // return new Vector2((float)(Amplitude * Math.Cos(_object.AngFre * TimeGame._gameTime + _object.AnBegin)), 0) + S0;
           
        //    time+=(float)(gameTime.ElapsedGameTime.Milliseconds)/1000.0f;
        //    listModel[0].Positon = Vector3.Zero + new Vector3(0,0,(float)(2000*Math.Cos(MathHelper.PiOver2*time)));
        //}
        //public void UpdateFreeCamera()
        //{
        //    if (Keyboard.GetState().IsKeyDown(Keys.S))
        //        ((FreeCamera)camera2).Move(4 * Vector3.Backward);
        //    if (Keyboard.GetState().IsKeyDown(Keys.W))
        //        ((FreeCamera)camera2).Move(4 * Vector3.Forward);
        //    if (Keyboard.GetState().IsKeyDown(Keys.A))
        //        ((FreeCamera)camera2).Move(4 * Vector3.Left);
        //    if (Keyboard.GetState().IsKeyDown(Keys.D))
        //        ((FreeCamera)camera2).Move(4 * Vector3.Right);
        //    camera2.Update();
        //}
        #endregion

        public void ResetGraphic()
        {
            GraphicsDevice.BlendState = BlendState.AlphaBlend;
            GraphicsDevice.DepthStencilState = DepthStencilState.None;
            GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
            GraphicsDevice.SamplerStates[0] = SamplerState.AnisotropicWrap;
        }
        public void BeginRender3D()
        {
            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
        }
       
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear( Color.CornflowerBlue);
            #region reset graphic
            ResetGraphic();
            #endregion
            
            #region 3D
            BeginRender3D();
            ship.Draw();
            ship.CreateBullet(Content, GraphicsDevice, gameTime);
            #endregion

            #region 2D
            spriteBatch.Begin();
            spriteBatch.DrawString(fontArial, "Position:\nX:" + ((int)ship.listShip[0].Positon.X).ToString() + "\nY:" + ((int)ship.listShip[0].Positon.Y).ToString() + "\nZ:" + ((int)ship.listShip[0].Positon.Z).ToString(), new Vector2(100, 0), Color.Red);
            spriteBatch.End();   
            #endregion

            base.Draw(gameTime);

        }
        
    }
}
